﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using HtmlAgilityPack;
using System.Net;
using System.Text.RegularExpressions;

namespace reptile
{
    public partial class Form1 : Form
    {

        private List<string> Urls = new List<string>();
        private List<string> phoneNumbers = new List<string>();
        private object lockObject = new object();

        public Form1()
        {
            InitializeComponent();
        }

        async private void button1_Click(object sender, EventArgs e)
        {
            string keyword = textBox1.Text;
            if (string.IsNullOrWhiteSpace(keyword))
            {
               MessageBox.Show("请输入关键字！");
               return;
            }

            Urls.Clear();
            listBox1.Items.Clear();

            await CrawlSearchResults(keyword, "https://www.bing.com");
        }

         async private Task CrawlSearchResults(string keyword, string searchEngine)
         {
            try
            {
                HttpClient client = new HttpClient();
                string url = $"{searchEngine}/search?q={keyword}";
                string response = await client.GetStringAsync(url);
                HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                document.LoadHtml(response);
                var resultLinks = document.DocumentNode.SelectNodes("//h2/a[@href]");
                if (resultLinks != null)
                {
                    List<Task> tasks = new List<Task>();

                    foreach (var link in resultLinks)
                    {
                        string Url = link.GetAttributeValue("href", "");
                        if (!string.IsNullOrWhiteSpace(Url) && !Urls.Contains(Url))
                        {
                            Urls.Add(Url);
                            AddUrlToListBox(Url);
                            tasks.Add(CrawlPhoneNumberAsync(Url));
                        }
                    }

                    await Task.WhenAll(tasks);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}");
            }

        }

        private async Task CrawlPhoneNumberAsync(string url)
        {
            phoneNumbers.Clear();
            await Task.Delay(100);
            WebClient webClient = new WebClient();
            string html = webClient.DownloadString(url);
            lock (lockObject)
            {
                string pattern = @"(\d{3}-\d{3}-\d{4})"; 
                MatchCollection matches = Regex.Matches(html, pattern);
                foreach (Match match in matches)
                {
                    phoneNumbers.Add(match.Value);
                }
            }        
            DisplayResults();
        }
            

        

        private void DisplayResults()
        {
            Invoke(new Action(() =>
            {
                foreach (string phoneNumber in phoneNumbers)
                {
                   listBox1.Items.Add(phoneNumber);
                }
            }));
        }

        private void AddUrlToListBox(string url)
        {
            Invoke(new Action(() =>
            {
                listBox2.Items.Add(url);
            }));
        }
    }
}
